package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.base.shiro.annotion.Permission;
import cn.stylefeng.guns.modular.system.controller.dto.IndentOrderTask;
import cn.stylefeng.guns.modular.system.controller.dto.SalesOrderTask;
import cn.stylefeng.guns.modular.system.entity.*;
import cn.stylefeng.guns.modular.system.model.params.IndentOrderCheckParam;
import cn.stylefeng.guns.modular.system.model.params.SalesOrderParam;
import cn.stylefeng.guns.modular.system.model.params.SalsOrderCheckParam;
import cn.stylefeng.guns.modular.system.model.params.paramsDTO.CheckDTO;
import cn.stylefeng.guns.modular.system.service.*;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.core.util.utility.StatusCode;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.apache.xmlbeans.impl.regex.REUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 订货单审核表控制器
 *
 * @author xiafei
 * @Date 2019-07-04 10:31:19
 */
@Controller
@RequestMapping("/salsOrderCheck")
public class SalsOrderCheckController extends BaseController {

    private String PREFIX = "/salsOrderCheck";

    @Autowired
    private SalsOrderCheckService salsOrderCheckService;
    @Autowired
    private AddressRoleService addressRoleService;

    @Resource
    private TaskService taskService;
    @Autowired
    private UserService userService;
    @Autowired
    private SalesOrderService salesOrderService;
    @Autowired
    private DealerLevelService dealerLevelService;
    @Autowired
    private LevelService levelService;
    @Autowired
    private DeptService deptService;


    /**
     * 跳转到主页面
     *
     * @author xiafei
     * @Date 2019-07-04
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/salsOrderCheck.html";
    }


    /**
     * 编辑页面
     *
     * @author xiafei
     * @Date 2019-07-04
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/salsOrderCheck_edit.html";
    }


    /**
     * 跳转到审核页面
     *
     * @return
     */
    @RequestMapping("/checkHtml")
    public String checkHtML() {
        return PREFIX + "/saleOrder_check.html";
    }

    /**
     * 跳转到退回页面
     *
     * @return
     */
    public String returnBackHtml() {
        return PREFIX + "/saleOrder_return.html";
    }

    /**
     * 删除接口
     *
     * @author xiafei
     * @Date 2019-07-04
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(SalsOrderCheckParam salsOrderCheckParam) {
        this.salsOrderCheckService.delete(salsOrderCheckParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author xiafei
     * @Date 2019-07-04
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(SalsOrderCheckParam salsOrderCheckParam) {
        SalsOrderCheck detail = this.salsOrderCheckService.getById(salsOrderCheckParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author xiafei
     * @Date 2019-07-04
     */
    @ResponseBody
    @RequestMapping("/list")
//    @Permission({"经销商审核人员", "工厂特价审批人员"})
    public LayuiPageInfo list(SalsOrderCheckParam salsOrderCheckParam) {

        Long addressId = addressRoleService.getOne(new QueryWrapper<AddressRole>()
                .eq("user_id", ShiroKit.getUserNotNull().getId())).getAddressId();

        if (ShiroKit.hasRole("工厂特价审批人员")) {
            return this.salsOrderCheckService.findPageBySpec(salsOrderCheckParam, addressId, StatusCode.SALSPENDING.getVal());
        } else {
            return this.salsOrderCheckService.findPageBySpec(salsOrderCheckParam, addressId, StatusCode.PENDING.getVal());
        }

    }


    /**
     * 审核界面
     *
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    public ResponseData check(CheckDTO checkDTO) {


        return null;
    }


    public ResponseData returnBack() {

        return null;
    }

    @ResponseBody
    @RequestMapping("/nomalList")
       public LayuiPageInfo list() {
        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey("pc_sales_order").taskCandidateUser(ShiroKit.getUserNotNull().getId() + "").list();
        if (tasks.size() == 0) {
            return new LayuiPageInfo();
        }

        //根据流程实例ID查询申请表单数据
        List<SalesOrderTask> salesOrderTasks = tasks.stream().filter(task -> salesOrderService.getByProcessInstanceId(task.getProcessInstanceId()) != null).map(task -> {
            SalesOrderTask salesOrderTask = new SalesOrderTask();
            BeanUtils.copyProperties(salesOrderService.getByProcessInstanceId(task.getProcessInstanceId()), salesOrderTask);
            salesOrderTask.setProcessInstanceId(task.getProcessInstanceId());
            if(StatusCode.PENDING.getVal()==salesOrderService.getByProcessInstanceId(task.getProcessInstanceId()).getStatus()){
                salesOrderTask.setStaus("待审核");
            } else if (StatusCode.SUCCESS.getVal() == salesOrderService.getByProcessInstanceId(task.getProcessInstanceId()).getStatus()){
                salesOrderTask.setStaus("审核成功");
            }
            salesOrderTask.setTaskId(task.getId());
            salesOrderTask.setTime(new Date());
            return salesOrderTask;
        }).collect(Collectors.toList());
        LayuiPageInfo result = new LayuiPageInfo();
        result.setCount(salesOrderTasks.size());
        result.setData(salesOrderTasks);
        return result;

//        /*Long addressId = addressRoleService.getOne(new QueryWrapper<AddressRole>()
//                .eq("user_id", ShiroKit.getUserNotNull().getId())).getAddressId();*/
//        Integer type = null;
//        if (ShiroKit.hasRole("订货单审核人员")) {
//            type = StatusCode.PENDING.getVal();
//        }else if (ShiroKit.hasRole("主管")){
//            type= StatusCode.INDENTSUCCESS.getVal();
//        }
//        return this.indentOrderCheckService.findPageBySpec(indentOrderCheckParam, type);
    }


    @ResponseBody
    @RequestMapping("/specialList")
    @Permission({"工厂主管审批人员"})
    public LayuiPageInfo specialList(SalesOrder salesOrder) {
        List<Task> tasks = taskService.createTaskQuery().processDefinitionKey("pc_special_sales_order").taskCandidateUser(ShiroKit.getUserNotNull().getId() + "").list();
        if (tasks.size() == 0) {
            return new LayuiPageInfo();
        }

        //根据流程实例ID查询申请表单数据
        List<SalesOrderTask> salesOrderTasks = tasks.stream().filter(task -> salesOrderService.getByProcessInstanceId(task.getProcessInstanceId()) != null).map(task -> {
            SalesOrderTask salesOrderTask = new SalesOrderTask();
            BeanUtils.copyProperties(salesOrderService.getByProcessInstanceId(task.getProcessInstanceId()), salesOrderTask);
            salesOrderTask.setProcessInstanceId(task.getProcessInstanceId());
            salesOrderTask.setTaskId(task.getId());
            return salesOrderTask;
        }).collect(Collectors.toList());
        LayuiPageInfo result = new LayuiPageInfo();
        result.setCount(salesOrderTasks.size());
        result.setData(salesOrderTasks);
        return result;

//        /*Long addressId = addressRoleService.getOne(new QueryWrapper<AddressRole>()
//                .eq("user_id", ShiroKit.getUserNotNull().getId())).getAddressId();*/
//        Integer type = null;
//        if (ShiroKit.hasRole("订货单审核人员")) {
//            type = StatusCode.PENDING.getVal();
//        }else if (ShiroKit.hasRole("主管")){
//            type= StatusCode.INDENTSUCCESS.getVal();
//        }
//        return this.indentOrderCheckService.findPageBySpec(indentOrderCheckParam, type);
    }

    @RequestMapping("/orderDetail")
    public String salesOrderDetail(Model model, Long id, String taskId){

        //经销商登录名
        ShiroUser shiroUser =  ShiroKit.getUserNotNull();
        model.addAttribute("username",shiroUser.getName());

        //部门
        String deptName = deptService.getById(shiroUser.getDeptId()).getSimpleName();
        model.addAttribute("deptName",deptName);
//        IndentOrderCheck indentOrderCheck = indentOrderCheckService.getById(id);
//        Long indentId = indentOrderCheck.getIndetOrderId();
        SalesOrder salesOrder = salesOrderService.getById(id);
        String ids = salesOrder.getPurchaseOrderId();
        model.addAttribute("ids",ids);
        model.addAttribute("id",id);
        model.addAttribute("taskId", taskId);
        return  PREFIX + "/saleCheckDetail.html";
    }

    @RequestMapping("/specialOrderDetail")
    public String specialOrderDetail(Model model, Long id, String taskId){

        //经销商登录名
        ShiroUser shiroUser =  ShiroKit.getUserNotNull();
        model.addAttribute("username",shiroUser.getName());

        //部门
        String deptName = deptService.getById(shiroUser.getDeptId()).getSimpleName();
        model.addAttribute("deptName",deptName);
//        IndentOrderCheck indentOrderCheck = indentOrderCheckService.getById(id);
//        Long indentId = indentOrderCheck.getIndetOrderId();
        SalesOrder salesOrder = salesOrderService.getById(id);
        String ids = salesOrder.getPurchaseOrderId();
        model.addAttribute("ids",ids);
        model.addAttribute("id",id);
        model.addAttribute("taskId", taskId);
        return  PREFIX + "/specialSaleCheckDetail.html";
    }

    @RequestMapping("/sendDetail")
    public String salesSendDetail(Model model, Long id, String taskId){

        //经销商登录名
        ShiroUser shiroUser =  ShiroKit.getUserNotNull();
        model.addAttribute("username",shiroUser.getName());

        //部门
        String deptName = deptService.getById(shiroUser.getDeptId()).getSimpleName();
        model.addAttribute("deptName",deptName);
//        IndentOrderCheck indentOrderCheck = indentOrderCheckService.getById(id);
//        Long indentId = indentOrderCheck.getIndetOrderId();
        SalesOrder salesOrder = salesOrderService.getById(id);
        String ids = salesOrder.getPurchaseOrderId();
        model.addAttribute("ids",ids);
        model.addAttribute("id",id);
        model.addAttribute("taskId", taskId);
        return  PREFIX + "/saleCheckSend.html";
    }


}

